home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
- From: Dan Pop <danpop@mail.cern.ch>
- Newsgroups: comp.lang.c
- Subject: Re: simple code, argc, argv, strcmp()
- Date: Tue, 6 Feb 1996 21:41:37 +0100
- Organization: CERN European Lab for Particle Physics
- Message-ID: <9602062041.AA18117@dxmint.cern.ch>
- References: <11f7cc$17261a.3b3@daprez> <9602011151.AA04526@dxmint.cern.ch> <31177B86.41C67EA6@texas.net>
- X-NNTP-Posting-Host: hpl3sn03.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
- X-Mail2News-Path: relay-4.mail.demon.net!post.demon.co.uk!dxmint.cern.ch!hpl3sn03.cern.ch
-
- Michael Douglass <mikedoug@texas.net> writes:
-
- >Dan Pop wrote:
- >
- >> So:
- >>
- >> 1. Never use ! with strcmp. It's a good way to shoot yourself in the foot.
- >
- >Hmmm... I've *never* had any problems with do that... Could you please
- >clarify why it would cause you to shoot yourself in the foot?
-
- The bunch of wrong posts in this very thread had already clarified my
- point :-)
-
- >> 2. Always check what a function is supposed to return before using it.
- >> strcmp returns 0 when the strings are equal.
- >
- >I think he knew that, he just had hist || and && logic backwards.
-
- Count yourself amongst the people who were fooled by his style :-)
- The original code looked like this:
-
- if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) Usage();
-
- Now, if we apply your "correction" the result is:
-
- if (!strcmp(argv[1],"-d") && !strcmp(argv[1],"-e")) Usage();
-
- which means that Usage() will be _never_ called, because this line is
- equivalent to
-
- if (strcmp(argv[1],"-d") == 0 && strcmp(argv[1],"-e") == 0) Usage();
-
- but argv[1], no matter its contents, cannot simultaneously compare equal
- to "-d" _and_ "-e"!
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-